package com.leigang.common.parent.utils;

import cn.hutool.core.collection.CollUtil;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ClassUtils {

        // 获取指定路径下文件夹中所有类的全限定类名
        public static List<String> getAllClasses(String folderPath) {
            List<String> classNames = new ArrayList<>();

            File folder = new File(folderPath);
            if (folder.exists() && folder.isDirectory()) {
                File[] files = folder.listFiles();
                if (files != null) {
                    for (File file : files) {
                        String path = file.getAbsoluteFile().getPath();
                        if (file.isFile() && path.endsWith(".java")) {
                            String className = path.substring(0,path.length()-5);
                            int index = className.indexOf("com/leigang");
                            if (index!= -1) {
                                className = className.substring(index);
                            }
                            className = className.replaceAll("/", "\\.");
                            classNames.add(className);
                        }
                    }
                }
            }

            return classNames;
        }

        // 通过类名获取类中所有属性
        public static List<String> getClassProperties(String className, List<String> propertyList) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
            List<String> properties = new ArrayList<>();
            Class<?> clazz = Class.forName(className);

            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                properties.add(field.getName());
            }
            if(CollUtil.isNotEmpty(propertyList)){
                for (Object enumConstant : clazz.getEnumConstants()) {
                    System.out.println("\n");
                    for (String s : propertyList) {
                        Field declaredField = clazz.getDeclaredField(s);
                        declaredField.setAccessible(true);
                        Object o = declaredField.get(enumConstant);
                        System.out.print(o+",");
                    }
                }
            }
            return properties;
        }

        public static void main(String[] args) throws Exception {
            // 指定路径
            String folderPath = "/Users/leigang/IdeaProjects/technology-cloud/technology-common/src/main/java/com/leigang/common/parent/enums"; // 例如："C:\\MyClasses"

            // 获取所有类名
            List<String> classNames = getAllClasses(folderPath);

            // 遍历类名并获取类中的属性
            for (String className : classNames) {
                List<String> properties = getClassProperties(className, null);
                List<String> propertyList = properties.stream()
                        .filter(property -> !isUpperCase(property) && !property.startsWith("$"))
                        .collect(Collectors.toList());
                getClassProperties(className, propertyList);
            }
        }

    public static boolean isUpperCase(String fieldName) {
        if (fieldName != null && !fieldName.isEmpty()) {
            char firstChar = fieldName.charAt(0);
            return Character.isUpperCase(firstChar);
        }
        return false;
    }

}
